//
//  ViewController.m
//  图片裁剪(圆头像图片)的实现
//
//  Created by hyl on 16/6/26.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //加载图片
    UIImage *image=[UIImage imageNamed:@"阿狸头像"];
    //开启图片上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //裁切
    //裁切范围
    //以下两种方法都可以
    UIBezierPath *path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(image.size.width*0.5, image.size.height*0.5) radius:image.size.width*0.5 startAngle:0 endAngle:M_PI*2 clockwise:YES];
//    UIBezierPath *path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    [path addClip];
    //[path fill];
    //绘制图片
    [image drawAtPoint:CGPointZero];
    //从上下文中获得裁切好的图片
    UIImage *image1=UIGraphicsGetImageFromCurrentImageContext();
    //关闭图片上下文
    UIGraphicsEndImageContext();
    //显示
    self.imageView.image=image1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
